package com.example.appbarlayoutdemo;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.appbarlayoutdemo.adapter.BrandCategoryAdapter;
import com.example.appbarlayoutdemo.adapter.ShopHotAdapter;
import com.example.appbarlayoutdemo.bean.CategoryListBean;
import com.example.appbarlayoutdemo.bean.DataListBean;
import com.example.appbarlayoutdemo.fragment.ShopClassifyFragment;
import com.example.appbarlayoutdemo.weight.DividerDecoration;
import com.example.appbarlayoutdemo.weight.GlideImageLoader;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private AppBarLayout appBarLayout;
    private Toolbar toolbar;
    private BrandCategoryAdapter brandCategoryAdapter;
    private RecyclerView rvShop, rvHot;
    private ShopHotAdapter hotAdapter;
    private List<DataListBean> listBeans;
    private ShopClassifyFragment shopClassifyFragment;
    private List<CategoryListBean> classifyList;
    private List<ShopClassifyFragment> classifyFragmentList;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private MyPagerAdapter adapter;
    private List<CategoryListBean> categoryListBeans;
    private Banner shopBanner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initBanner();
        setAvatorChange();
        initData();
        initShopListAdapter();
        initHotAdapter();
        initTab();
    }

    private void initView() {
        appBarLayout = findViewById(R.id.appbar_layout);
        toolbar = findViewById(R.id.toolbar);
        rvShop = findViewById(R.id.rv_shop);
        rvHot = findViewById(R.id.rv_hot);
        viewPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);
        shopBanner = findViewById(R.id.iv_banner);
    }

    private void setAvatorChange() {
        appBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
            //verticalOffset始终为0以下的负数
            float percent = (Math.abs(verticalOffset * 1.0f) / appBarLayout.getTotalScrollRange());
            toolbar.setBackgroundColor(MainActivity.this.changeAlpha(Color.WHITE, percent));
        });
    }

    private void initData() {
        listBeans = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            DataListBean dataListBean = new DataListBean();
            dataListBean.setGoodsName("短款皮草外套");
            dataListBean.setSellingPrice("8800.0");
            listBeans.add(dataListBean);
        }
    }

    private void initBanner() {
        List<String> list = new ArrayList<>();
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");

        //设置banner样式
        shopBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        shopBanner.setImageLoader(new GlideImageLoader());
        //设置banner动画效果
        shopBanner.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        shopBanner.isAutoPlay(true);
        //设置轮播时间
        shopBanner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        shopBanner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        shopBanner.setImages(list);
        shopBanner.start();
    }

    private void initShopListAdapter() {
        brandCategoryAdapter = new BrandCategoryAdapter(listBeans);
        rvShop.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
        rvShop.addItemDecoration(new DividerDecoration(this.getResources().getColor(R.color.white), 8));
        rvShop.setAdapter(brandCategoryAdapter);
    }

    private void initTab() {
        classifyList = new ArrayList<>();
        classifyFragmentList = new ArrayList<>();
        categoryListBeans = new ArrayList<>();
        for(int i=0;i<10;i++){
            CategoryListBean categoryListBean  = new CategoryListBean();
            categoryListBean.setCategoryName("帽子");
            categoryListBean.setCategoryId("10011");
            categoryListBeans.add(categoryListBean);
        }
        setTab(categoryListBeans);
    }

    private void initHotAdapter() {
        hotAdapter = new ShopHotAdapter(listBeans);
        rvHot.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,false));
        rvHot.addItemDecoration(new DividerDecoration(this.getResources().getColor(R.color.white), 8));
        rvHot.setAdapter(hotAdapter);
    }

    private void setTab(List<CategoryListBean> data) {
        try {
            if (data != null && data.size() > 0) {
                classifyList.addAll(data);
                for (int i = 0; i < classifyList.size(); i++) {
                    shopClassifyFragment = new ShopClassifyFragment();
                    classifyList.get(0).setCheck(true);
                    classifyFragmentList.add(shopClassifyFragment);
                    tabLayout.addTab(tabLayout.newTab());
                }
                adapter = new MyPagerAdapter(getSupportFragmentManager());
                viewPager.setAdapter(adapter);
                tabLayout.setupWithViewPager(viewPager);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据百分比改变颜色透明度
     */
    public int changeAlpha(int color, float fraction) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        int alpha = (int) (Color.alpha(color) * fraction);
        return Color.argb(alpha, red, green, blue);
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public ShopClassifyFragment getItem(int position) {
            return classifyFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return classifyFragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return classifyList.get(position).getCategoryName();
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        }
    }
}